/*
 SGRecyclableItem.m
 Date Created: March 6, 2010
 
 Authors: Karanvir Gill <viper1029@gmail.com>
          Frank Lau <flaua@sfu.ca>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "SGRecyclableItem.h"
#define WIDTH_OF_BACKGROUND 100
#define HEIGHT_OF_BACKGROUND 100

@implementation SGRecyclableItem
@synthesize isBeingDragged;
@synthesize hasBeenDropped;
@synthesize isLifeGiver;
@synthesize depositValue;
@synthesize uniqueName;

- (SGRecyclableItem*)initWithType:(SGRecyclableType*)itemType xLocation:(float)xCoor yLocation:(float)yCoor givesLife:(BOOL)lifeGiver{
	self = [super init];
	if( self ) {
		// Create temporary image to determine the size of the frame to create
		UIImage *tempImage = [UIImage imageNamed:itemType.image];
		
		if(lifeGiver){// Add a background if item is lifegiver
			UIImage *background = [UIImage imageNamed:@"lifegiverfade.png"];
			tempImage = [self combineImage:tempImage toBackground:background];
		}
		
		[self initWithFrame:CGRectMake(xCoor, yCoor, tempImage.size.width, tempImage.size.height)];
		[self setImage:tempImage];
		self.center = CGPointMake(xCoor,yCoor);
		
		// Setup attributes of this item 
		uniqueName = [itemType uniqueName];
		depositValue = [itemType depositValue];
		isBeingDragged = false;
		hasBeenDropped = false;
		isLifeGiver = lifeGiver;
	}
	return self;
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
  // Perform the move
	CGPoint currentLocation = [[touches anyObject] locationInView:self];
	CGRect frame = [self frame];
	
	frame.origin.x += currentLocation.x - startLocation.x;
	frame.origin.y += currentLocation.y - startLocation.y;
	[self setFrame:frame];
}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
	isBeingDragged = true;
	CGPoint currentLocation = [[touches anyObject] locationInView:self]; 
	startLocation = currentLocation;
	[[self superview] bringSubviewToFront:self];
}

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
  NSLog(@"END");
	isBeingDragged = false;
	hasBeenDropped = true;
}

- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
  NSLog(@"CANCEL");
  isBeingDragged = false;
  hasBeenDropped = true;
}

- (UIImage*) combineImage:(UIImage*)image1 toBackground:(UIImage*)image2{
	
	UIGraphicsBeginImageContext(CGSizeMake(WIDTH_OF_BACKGROUND,HEIGHT_OF_BACKGROUND));  
	
	// Draw the recyclabe item
	[image1 drawInRect:CGRectMake(WIDTH_OF_BACKGROUND/2-image1.size.width/2, HEIGHT_OF_BACKGROUND/2-image1.size.height/2, image1.size.width, image1.size.height)];  
	
	// Draw the back ground  
	[image2 drawInRect:CGRectMake(0, 0, WIDTH_OF_BACKGROUND, HEIGHT_OF_BACKGROUND)];
	
	UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();  
	
	UIGraphicsEndImageContext();  
	
	return combinedImage;
}

@end
